package com.bana.fvs.task;

import com.bana.fvs.code.ParameterKeys;
import com.bana.fvs.protocol.*;
import com.bana.fvs.model.*;
import com.bana.fvs.service.ControllerService;
import com.bana.fvs.service.FogAreaService;

import com.bana.fvs.protocol.Notice;
import com.bana.fvs.utils.LampServer;
import com.bana.fvs.webservice.GateWayServiceImplPortImpl;
import com.bana.sys.service.LogService;
import com.bana.sys.service.ParameterService;
import com.bana.sys.socket.*;
import com.bana.sys.utils.Convert;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import javax.xml.ws.Endpoint;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

/**
 * 通信服务连接
 */
@Configuration
public class SocketTask implements Notice.NoticeListener {

    @Resource
    private ParameterService parameterService;

    @Resource
    private LogService logService;

    @Resource
    private FogAreaService fogAreaService;

    @Resource
    private ControllerService controllerService;

    @Resource
    private GateWayServiceImplPortImpl gateWayServiceImplPort;


    /**
     * 命令下发
     */
    public static ClientChannel ProtocolHandler=null;

    /**
     * 通知
     */
    private static ClientChannel NoticeHandler=null;

    /**
     * 联动设备
     */
    public static ClientChannel DeviceHandler=null;

    private NoticeLampStatus noticeLampStatus=new NoticeLampStatus();

    private NoticeVisibilityStatus noticeVisibilityStatus=new NoticeVisibilityStatus();

    private NoticeRoadStatus0A noticeRoadStatus0A =new NoticeRoadStatus0A();

    private NoticeRoadStatus0C noticeRoadStatus0C =new NoticeRoadStatus0C();

    private NoticeWeatherStatus08 noticeWeatherStatus08 =new NoticeWeatherStatus08();

    private NoticeWeatherStatus0E noticeWeatherStatus0E =new NoticeWeatherStatus0E();

    private NoticeCrash10 noticeCrash10=new NoticeCrash10();

    private Notice notice =new Notice();

    @Scheduled(fixedDelay=5000)
    public void handle(){
        if(ProtocolHandler==null)
        {
            String ip=parameterService.getString(ParameterKeys.ServiceIp);
            int routerPort=parameterService.getInteger(ParameterKeys.RouterPort);
            int noticePort=parameterService.getInteger(ParameterKeys.NoticePort);
            ProtocolHandler=new ClientChannel(ip, routerPort);
            NoticeHandler=new ClientChannel(ip,noticePort);
            NoticeHandler.addProtocol(notice);
            notice.addListener(this);
            String deviceIp=parameterService.getString(ParameterKeys.DeviceServiceIp);
            if(!"-".equalsIgnoreCase(deviceIp))
            {
                DeviceHandler=new ClientChannel(deviceIp, parameterService.getInteger(ParameterKeys.DeviceServicePort));
            }
            Calendar calendar= Calendar.getInstance();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            logService.insertSystemLog("当前版本:1.1.0");
            logService.insertSystemLog(String.format("当前时间:%s",dateFormat.format(calendar.getTime())));
            logService.insertSystemLog(String.format("当前时区:%s", TimeZone.getDefault().getDisplayName()));
            try
            {
                String address = "http://0.0.0.0:8081/fogService/services/services/gateWayService";
                Endpoint.publish(address, gateWayServiceImplPort);
                logService.insertSystemLog("成功打开webservice");
            }
           catch (Exception ex)
           {

           }
        }

        if(!ProtocolHandler.getConnected())
        {
            ProtocolHandler.connect();
        }
        if(!NoticeHandler.getConnected())
        {
            NoticeHandler.connect();
        }

        if(DeviceHandler!=null&&!DeviceHandler.getConnected())
        {
            DeviceHandler.connect();
        }
    }


    @Override
    public void update(int port, byte[] buffer)  {

        try
        {
            if(buffer[2]==noticeVisibilityStatus.getId())
            {
                FogArea fogArea=noticeVisibilityStatus.toModel(buffer);
                fogArea.setJcydk(port);
                if(fogArea!=null)
                {
                    fogAreaService.updateNoticeVisibility(fogArea);
                    logService.insertSocketLog(noticeVisibilityStatus.getId(),fogArea.getJcybsm(),port, Convert.toHex(buffer));
                }
            }
            else if(buffer[2]== noticeRoadStatus0C.getId())
            {
                FogArea road= noticeRoadStatus0C.toModel(buffer);
                road.setJcydk(port);
                fogAreaService.updateRoad(road);
                logService.insertSocketLog(noticeRoadStatus0C.getId(),road.getJcybsm(),port, new String(buffer));
            }
            else if(buffer[2]== noticeWeatherStatus08.getId())
            {
                FogArea weather= noticeWeatherStatus08.toModel(buffer);
                weather.setJcydk(port);
                fogAreaService.updateWeather(weather);
                logService.insertSocketLog(noticeWeatherStatus08.getId(),weather.getJcybsm(), port,new String(buffer));
            }
            else if(buffer[2]== noticeWeatherStatus0E.getId())
            {
                FogArea weather= noticeWeatherStatus0E.toModel(buffer);
                weather.setJcydk(port);
                fogAreaService.updateWeather(weather);
                logService.insertSocketLog(noticeWeatherStatus0E.getId(),weather.getJcybsm(),port, new String(buffer));
            }
            else if(buffer[2]== noticeRoadStatus0A.getId())
            {
                FogArea road= noticeRoadStatus0A.toModel(buffer);
                road.setJcydk(port);
                fogAreaService.updateRoad(road);
                logService.insertSocketLog(noticeRoadStatus0A.getId(),road.getJcybsm(),port, new String(buffer));
            }
            else if(buffer[2]==noticeCrash10.getId())
            {
                Lamp lamp=noticeCrash10.toModel(buffer);
                lamp.setKzqdk(port);
                controllerService.updateLampCache(lamp);
                LampServer.sendAll(logService,lamp);
                logService.insertSocketLog(noticeCrash10.getId(),lamp.getKzqbsm(),port, Convert.toHex(buffer));
            }
        } catch (IOException e)
        {

        }
    }
}
